//
//  ReadingViewController.m
//  SunXi
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 大远. All rights reserved.
//

#import "ReadingViewController.h"


@interface ReadingViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,ReadingFirstPageHeaderViewdelegate>

@property(nonatomic, retain)UICollectionViewFlowLayout *flowLayout;

@property(nonatomic, retain)NSMutableArray *arrCell;

@property(nonatomic, retain)NSMutableArray *arrScrol;

@end

@implementation ReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"阅读";
    SV.backgroundColor = [UIColor whiteColor];
    [self createData];
    
        
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - 创建 collectionView

- (void)createCollection{
  

#if 1
    //      瀑布流
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.minimumLineSpacing = HEIGHT / 100;
    self.flowLayout.minimumInteritemSpacing = WIDTH / 100 * 1;
    //    self.flowLayout.itemSize = CGSizeMake(0, 0);
    //    self.flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //       collectionView 的创建
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:self.flowLayout];
    [SV addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.collection.backgroundColor = [UIColor whiteColor];
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(SV).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    //      设置头部的滚动区域范围
    //      头部
    self.flowLayout.headerReferenceSize = CGSizeMake(WIDTH, HEIGHT / 100 * 30);
    //      尾部
    self.flowLayout.footerReferenceSize = CGSizeMake(WIDTH, HEIGHT / 100 * 25);
    
    //      collectionView 要创建cell 的话,只能使用注册的方式
    [self.collection registerClass:[ReadingFirstPageCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //      collectionView 的头视图的注册
    //      注册头部区域
    //      参数1. 注册对象类型
    //      参数2. 需要注册部分是头部还是尾部
    //      参数3. 重用标志
    [self.collection registerClass:[ReadingFirstPageHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    [self.collection registerClass:[ReadingFirstPageFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
#endif
}



/* 定义每个UICollectionView 的大小 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(WIDTH / 100 * 31, HEIGHT / 100 * 20);
}
/* 定义每个UICollectionView 的边缘 */
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(HEIGHT / 100, WIDTH / 100 * 2, HEIGHT / 100, WIDTH / 100 * 2);//上 左 下 右
}





//      collectionView 的返回的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrCell.count;
}


//      collectionView 的 cell 的创建
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadingFirstPageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor cyanColor];
    cell.model = self.arrCell[indexPath.row];
    return cell;
}



//      collectionView 的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ReadingDetailViewController *VC = [[ReadingDetailViewController alloc] init];
    VC.model = self.arrCell[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
}


    //      关于头足视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    //      关于头视图
    if (kind == UICollectionElementKindSectionHeader) {
        
        ReadingFirstPageHeaderView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor grayColor];
        reusableView.delegate = self;
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < self.arrScrol.count; i++) {
            [arr addObject:self.arrScrol[i][@"img"]];
        }
        reusableView.arr = arr;
        reusableView.VC = self;
        NSLog(@"%@", self.arrScrol[indexPath.row][@"url"]);
        return reusableView;
    }
    //      关于足视图
    
    if (kind == UICollectionElementKindSectionFooter) {
        ReadingFirstPageFooterView *reuese = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
        reuese.label.text = @"24小时写作";
        reuese.labelEnglish.text = @"New Writing";
        reuese.VC = self;
        return reuese;
    }
    
    return nil;
}




#pragma mark - 解析数据
- (void)createData{
    
    [ShowMytext showHUDloadView:SV];
    
    NSString *str = @"http://api2.pianke.me/read/columns";
    NSString *para = @"client=2";
    [DyAFnetWorking cachePostWithURL:str WithbodyStr:para success:^(id result) {
        NSLog(@"%@", result);
        //      collectionView 接收数据
        self.arrCell = [ReadingFirstModel baseModelByArr:result[@"data"][@"list"]];
        self.arrScrol = result[@"data"][@"carousel"];
        NSLog(@"%@", self.arrScrol);
        [self.collection reloadData];
        [self createCollection];
        [ShowMytext hiddHUDloadView];
        
    } failure:^(id nonet) {
        
        
    }];

}
- (void)takeVale:(NSInteger)value{
    [ShowMytext showHUDloadView:SV];
    NSString *strtemp  = self.arrScrol[value][@"url"];
    NSArray *atrArr = [strtemp componentsSeparatedByString:@"/"];
    NSString *Pahe = [atrArr lastObject];
    NSString *str = @"http://api2.pianke.me/article/info";
    NSDictionary *para = @{@"contentid":Pahe,@"client":@"2"};
    [DyAFnetWorking POSTAfnNetworkingWithUrl:str WithParameters:para Block:^(id result) {
        WebView *web = [[WebView alloc]init];
        web.html = NO;
        web.tittle  =result[@"data"][@"shareinfo"][@"title"];
        web.url = result[@"data"][@"shareinfo"][@"url"];
        [self.navigationController pushViewController:web animated:YES];
        [ShowMytext hiddHUDloadView];

    }];
    
    
}


@end
